﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;

namespace WebStoreClient.Test
{
    class Program
    {
        static WebStoreClient client = new WebStoreClient(new Uri("http://eccloudt.bjdhxw.com"), TimeSpan.FromSeconds(5), "/EcCloudApi/api/Sys_Token", "/EcCloudApi/api/Sys_StoreProduSafe");
        static void Main(string[] args)
        {
            System.AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
            
            Console.WriteLine("测试WebStoreCLient");

            ErrorMsg msg = new ErrorMsg();
            if (client.Login("T1_ADMIN", "888888", ref msg))
            {
                Console.WriteLine("登陆成功");
            }
            else
            {
                Console.WriteLine("登陆失败," + msg.Message + "," + msg.ExceptionMessage);
            }
            client.LogWriter = new Action<string>(str =>
              {
                  Console.WriteLine("wsClient内部日志:" + str);
              });
            int count = 50;
            while (count-- > 0)
            {
                Console.WriteLine("------------------------测试单储存过程调用------------------------");
                StoreProduResult result = client.StoreProduExecute(new StoreProdu()
                {
                    SpName = "sp_Sys_FlowCatalog_Select",
                    SpInParams = new Dictionary<string, object>()
                {
                    {"Keyword","" },
                    {"USERCODE",null }
                },
                    SpOutParams = new Dictionary<string, object>()
                {
                    {"CheckMessage","" }
                }
                });
                Console.WriteLine($"获取数据CheckMessage为:{result.SpOutParams["CheckMessage"]}");
                DataTable dt = result.SpDataSet.Tables[0];
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    Console.WriteLine($"获取数据{i}行第一列:{dt.Rows[i][0]}");
                }

                Console.WriteLine("------------------------测试多储存过程调用------------------------");
                
                if (count == 48)
                {//故意将凭据去掉，触发需要用户重新登陆的异常
                    client.Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic");
                    Console.WriteLine("故意将凭据去掉，触发需要用户重新登陆的异常");
                }
                Thread.Sleep(TimeSpan.FromSeconds(3));


            }
            Console.ReadLine();
        }

        private static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
        {
            if(e.ExceptionObject is WsNeedLoginException)
            {
                login:
                Console.WriteLine("身份错误，请录入您的身份信息!" + Environment.NewLine + "用户名 :");
                var username = Console.ReadLine();
                Console.WriteLine("密码 :");
                var password = Console.ReadLine();
                ErrorMsg error = new ErrorMsg();
                if(client.Login(username,password,ref error))
                {
                    Console.WriteLine("登陆成功");
                }
                else
                {
                    Console.WriteLine("登陆失败," + error.Message + Environment.NewLine + error.ExceptionMessage);
                    
                }
                goto login;
            }
            else if(e.ExceptionObject is WsForbiddenException)
            {
                Console.WriteLine("[错误]您无访问此接口的权限");
            }
            else
            {
                Console.WriteLine("[错误]未经处理的异常");
            }
        }
    }
}
